package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    private GeofenceMapper geofenceMapper;

    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectListGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询围栏参数: geofenceQuery:{}",geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectList(geofenceQuery);
        // GeofenceVO缺少两个数据,要进行补充 vehicleList(围栏绑定车辆列表) totalNum(围栏绑定车辆总数)
        for (GeofenceVO geofenceVO : list) {
            // 依次取出每个遍历到的围栏对象
            // 查询当前围栏上绑定的所有车辆
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            // 查询出来指定与制定围栏Id对应的车辆列表
            List<VehicleVO> vehicleVOList = vehicleMapper.selectvehicle(vehicleQuery);
            //     获取当前围栏的绑定车辆总数
            int totalNum = vehicleVOList.size();
            int availableNum = 0;
            for (VehicleVO vehicleVO : vehicleVOList) {
                if (vehicleVO.getStatus().equals("1")) {
                    availableNum++;
                }
            }
            if (availableNum > 0) {
                geofenceVO.setAvailableNum(availableNum);
            } else {
                geofenceVO.setAvailableNum(0);
            }
            //     给围栏对象设置参数
            geofenceVO.setTotalNum(totalNum);
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam,geofence);
    //     前端数据是KEY - VALUE的结构
        HashMap<String,String> position = geofenceParam.getPosition();
    //     JSON处理库工具
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            // 这个方法如果传入null值或字段不匹配会抛出异常,所以放置于try-catch中处理即可
            String positionStr = objectMapper.writeValueAsString(position);
            if (geofenceParam.getId()!=null){//更新围栏
                geofenceMapper.update(geofence);
            }else { //新增围栏
                geofence.setStatus("1");
                geofence.setPosition(positionStr);
                geofence.setCreateTime(new Date());
                geofenceMapper.insert(geofence);
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteGeofence(Long id) {
        VehicleQuery vehicleQuery= new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> list = vehicleMapper.selectvehicle(vehicleQuery);
        if (!list.isEmpty()){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }else {
            geofenceMapper.deleteByGeofenceId(id);
        }

    }
}
